package com.yyon.mosaikan;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.timer.ITimerCallback;
import org.anddev.andengine.engine.handler.timer.TimerHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.content.Intent;

public class MenuScreen extends BaseGameActivity implements IOnSceneTouchListener, ITimerCallback { // activity called when game starts

    final int CAMERA_WIDTH = 480;
    final int CAMERA_HEIGHT = 320;
	private Camera camera;
	private Texture backgroundtexture;
	private TextureRegion background;
	private TextureRegion instructions;
	private Sprite instructionsprite;
	private Scene scene;
	private Texture aebadgetexture;
	private TextureRegion aebadgeregion;
	private Sprite andenginebadge;
	private boolean isshowingbadge = true;
	private boolean isshowinghelp = true;
	private Rectangle andenginebadgebackground;
	private Texture playgamebuttontexture;
	private Texture optionsbuttontexture;
	private TextureRegion playgamebuttonregion;
	private TextureRegion optionsbuttonregion;
	private int buttonseparation = CAMERA_HEIGHT/6;
	private Texture instructionstexture;
	private Sprite playgamebutton;
	private Sprite optionsbutton;
	private Texture mosaikanbadgetexture;
	private TextureRegion mosaikanbadgeregion;
	private Sprite mosaikanbadge;
	private float bscale = 0.7f;
	private float mosaikanbadgescale = 0.25f;

	@Override
    public Engine onLoadEngine() {
		this.camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); // start andengine engine
    	return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.camera).setNeedsSound(true));
    }

	@Override
	public void onLoadResources() {
    	TextureRegionFactory.setAssetBasePath("gfx/"); // images come from /assets/gfx

    	this.backgroundtexture = new Texture(512,512, TextureOptions.DEFAULT); //background
        this.background = TextureRegionFactory.createFromAsset(this.backgroundtexture, this, "trees.png", 0, 0);
    	this.instructionstexture = new Texture(512,512, TextureOptions.DEFAULT); //instructions
        this.instructions = TextureRegionFactory.createFromAsset(this.instructionstexture, this, "mosaikaninstructions3.png", 0, 0);

        this.aebadgetexture = new Texture(256,256, TextureOptions.BILINEAR_PREMULTIPLYALPHA); //andengine badge
        this.aebadgeregion = TextureRegionFactory.createFromAsset(this.aebadgetexture, this, "andengine_badge.png", 0, 0);

        this.playgamebuttontexture = new Texture(256,128, TextureOptions.BILINEAR_PREMULTIPLYALPHA); // play game button
        this.playgamebuttonregion = TextureRegionFactory.createFromAsset(this.playgamebuttontexture, this, "playgamebutton.png", 0, 0);
        this.optionsbuttontexture = new Texture(256,128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);// options button
        this.optionsbuttonregion = TextureRegionFactory.createFromAsset(this.optionsbuttontexture, this, "optionsbutton.png", 0, 0);

        this.mosaikanbadgetexture = new Texture(512,512, TextureOptions.BILINEAR_PREMULTIPLYALPHA); // make mosaikan badge picture
        this.mosaikanbadgeregion = TextureRegionFactory.createFromAsset(this.mosaikanbadgetexture, this, "hi-res-pic.png", 0, 0);
        
        // load all textures
        this.mEngine.getTextureManager().loadTextures(this.backgroundtexture, this.aebadgetexture, this.playgamebuttontexture, this.optionsbuttontexture, this.instructionstexture, this.mosaikanbadgetexture);
	}

	@Override
	public Scene onLoadScene() {
        scene = new Scene(); // create scene 
        
        scene.attachChild(new Sprite(0,0,this.background)); // draw background
        // draw play game button
        this.playgamebutton =new Sprite(50-playgamebuttonregion.getWidth()/2+playgamebuttonregion.getWidth()*bscale/2,CAMERA_HEIGHT/2-this.playgamebuttonregion.getHeight()/2-buttonseparation,this.playgamebuttonregion);
        this.playgamebutton.setScale(bscale); 
        scene.attachChild(this.playgamebutton);  
        // draw options button
        this.optionsbutton = new Sprite(50-optionsbuttonregion.getWidth()/2+optionsbuttonregion.getWidth()*bscale/2,CAMERA_HEIGHT/2-this.optionsbuttonregion.getHeight()/2+buttonseparation,this.optionsbuttonregion);
        this.optionsbutton.setScale(bscale);
        scene.attachChild(this.optionsbutton);
        // draw mosaikan badge
        this.mosaikanbadge = new Sprite(-50+CAMERA_WIDTH-mosaikanbadgeregion.getWidth()/2+mosaikanbadgeregion.getWidth()*mosaikanbadgescale/2-mosaikanbadgeregion.getWidth()*mosaikanbadgescale, CAMERA_HEIGHT/2-this.mosaikanbadgeregion.getHeight()/2, this.mosaikanbadgeregion);
        this.mosaikanbadge.setScale(mosaikanbadgescale);
        scene.attachChild(this.mosaikanbadge);
        
        // draw instructions
        instructionsprite = new Sprite(0,0,this.instructions);
        scene.attachChild(instructionsprite);

        // draw andengine badge (covers up everything else)
        andenginebadgebackground = new Rectangle(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
        andenginebadgebackground.setColor(0,0,0);
        scene.attachChild(andenginebadgebackground);

        andenginebadge = new Sprite(CAMERA_WIDTH/2-75,CAMERA_HEIGHT/2-75,this.aebadgeregion);
        scene.attachChild(andenginebadge);

        scene.setOnSceneTouchListener(this); // accept touching of screen
        scene.setTouchAreaBindingEnabled(true);
        scene.registerTouchArea(instructionsprite);
        
        scene.registerUpdateHandler(new TimerHandler(2, false, this)); // timer for andengine badge to go away (2 seconds)
         
		return scene;
	}

	@Override
	public void onLoadComplete() {

	}
	
	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) { // if someone touched the screen
		if (this.isshowingbadge == false && this.isshowinghelp == false) { // if showing the main menu screen
			if (pSceneTouchEvent.getX()>playgamebutton.getX() && pSceneTouchEvent.getX()<playgamebutton.getX()+playgamebutton.getWidth() && pSceneTouchEvent.getY()>playgamebutton.getY() && pSceneTouchEvent.getY()<playgamebutton.getY()+playgamebutton.getHeight()) {
			    MenuScreen.this.startActivity(new Intent(MenuScreen.this, Mosaikan.class)); // if touched play button, start game and close menu
			    MenuScreen.this.finish();
			} else if (pSceneTouchEvent.getX()>optionsbutton.getX() && pSceneTouchEvent.getX()<optionsbutton.getX()+optionsbutton.getWidth() && pSceneTouchEvent.getY()>optionsbutton.getY() && pSceneTouchEvent.getY()<optionsbutton.getY()+optionsbutton.getHeight()) {
			    MenuScreen.this.startActivity(new Intent(MenuScreen.this, Preferences.class)); // if touched options button, start options menu (Preferences.java)
			}
		} else if (this.isshowingbadge == false && this.isshowinghelp == true && pSceneTouchEvent.isActionUp()) { // if showing help screen and touched
			this.isshowinghelp = false;
			this.instructionsprite.setVisible(false); // remove instructions to reveal main menu
		}
		return true;
	}

	@Override
	public void onTimePassed(TimerHandler pTimerHandler) { // remove andengine badge after timer expires
		andenginebadge.setVisible(false);
		andenginebadgebackground.setVisible(false);
		this.isshowingbadge=false;
	}
	
}